<template>
  <div class="list_container flex both-100">
    <Breadcrumb :title="pageTitle" />
    <div class="content_container">
      <!-- 搜索模块 -->
      <el-form ref="searchMap" :inline="true" :model="searchMap" class="serach_wrapper flex-space-between">
        <div class="search">
          <el-form-item prop="deviceTypeName">
            <el-input v-model="searchMap.deviceTypeName" placeholder="设备类型名称" size="small"></el-input>
          </el-form-item>

          <el-form-item class="flex">
            <el-button @click="handleSearch()" class="btn_primary">查询</el-button>
            <el-button @click="handleReset()" class="btn_cancel">重置</el-button>
          </el-form-item>
        </div>
        <div class="edit">
          <el-form-item class="flex">
            <el-button @click="handleAdd" class="btn_primary" icon="el-icon-plus"> 新增 </el-button>
            <el-button @click="handleDelete" class="btn_primary" icon="el-icon-delete"> 删除 </el-button>
          </el-form-item>
        </div>
      </el-form>
      <!-- 搜索模块over -->

      <!-- 表格模块 -->
      <div class="table_container">
        <el-table ref="table" :data="dataList" v-loading="loading" stripe class="per_table" @selection-change="handleSelectionChange" style="width: 100%" row-key="deviceTypeId" @expand-change="handleExpandChange" :load="loadChildren" lazy :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="设备类型字符串" align="center" prop="deviceTypeKey" :show-overflow-tooltip="true" />
          <el-table-column label="设备类型名称" align="center" prop="deviceTypeName" :show-overflow-tooltip="true" />
          <el-table-column label="设备分类对应的数量" align="center" prop="num" :show-overflow-tooltip="true" />
          <el-table-column label="排序" align="center" prop="sort" :show-overflow-tooltip="true" />
          <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"> </el-table-column>
          <el-table-column label="操作" width="150" fixed="right">
            <template slot-scope="scope">
              <div class="button-group">
                <span type="text" class="tag_primary" size="small" @click="handleEdit(scope.row)">编辑</span>
                <!-- <span type="text" class="tag_warning" size="small" @click="handleDelete(scope.row)">删除</span> -->
                <span type="text" class="tag_primary" size="small" @click="handleDetail(scope.row)">详情</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 表格模块over -->

      <!-- 分页 -->
      <!-- <Pagination v-show="total > 0" :total="total" :page.sync="searchMap.pageNum" :limit.sync="searchMap.pageSize" @change="getList"></Pagination> -->
      <!-- 分页over -->
    </div>

    <!-- 弹窗 新增/编辑 -->
    <typesForm :formData="formData" :dictMap="dict" :titleIndex="titleIndex" :dialogVisible="dialogVisible" @cancel="cancel" @getList="getList"></typesForm>
    <!-- 弹窗 新增/编辑 over -->

    <!-- 弹窗 详情 -->
    <TypesDetail :descData="descData" :dictMap="dict" :dialogVisible="dialogVisibleDetail" @cancel="cancelDetail"></TypesDetail>
    <!-- 弹窗 详情 over -->
  </div>
</template>

<script>
import { listDeviceTypesData, getDeviceTypesData, addDeviceTypesData, updateDeviceTypesData, delDeviceTypesData, listDeviceTypesDataTree } from '@/api/system/device/types';
import TypesForm from '@/views/system/device/TypesForm';
import TypesDetail from '@/views/system/device/TypesDetail';

export default {
  name: 'DeviceTypes',
  dicts: ['device_status', 'device_enable_status'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 搜索条件
      searchMap: {
        // pageNum: 1,
        // pageSize: 10,
        // deviceTypeKey: '',
        deviceTypeName: '',
        pid: 0,
        // remark: '',
        // sort: 0
      },
      // 数据列表
      dataList: [],
      // 总条数
      total: 0,
      // 表单字段数据
      formData: {
        pid: 0,
        deviceTypeId: '',
        deviceTypeKey: '',
        deviceTypeName: '',
        sort: 0,
        num: '',
        remark: '',
        dataList: []
      },
      descData: {},
      // 弹窗-新增，编辑头文字
      titleIndex: 0,
      // 弹窗-新增/编辑-显示隐藏
      dialogVisible: false,
      // 弹窗-详情-显示隐藏
      dialogVisibleDetail: false,
    };
  },
  created() {
    this.getList();
  },
  components: {
    TypesForm,
    TypesDetail,
  },
  computed: {
    pageTitle() {
      return '设备类型';
    },
  },
  methods: {
    getList() {
      this.loading = true;
      listDeviceTypesData(this.searchMap).then(res => {
        if (200 === res.code) {
          let treeData = this.$refs.table.store.states.treeData;
          this.dataList = res.data.map((item, index) => {
            if (item.leaf == '0') {
              item.hasChildren = true;
            }
            // 默认加载数据时候所有含有子节点的加载都为关闭状态
            if(treeData[item.deviceTypeId]){
              treeData[item.deviceTypeId].loaded = false
              treeData[item.deviceTypeId].expanded = false
            }
            return item;
          });
          this.total = res.data.length;
        }
        this.loading = false;
      });
    },
    /** 查询按钮操作 */
    handleSearch() {
      this.getList();
    },
    /** 重置按钮操作 */
    handleReset() {
      this.resetForm('searchMap');
      this.searchMap.pid = 0
      this.getList()
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deviceTypeId);
      this.multiple = !selection.length;
    },
    // 新增
    handleAdd() {
      this.titleIndex = 0;
      this.dialogVisible = true;
      this.reset();
    },
    // 修改数据
    handleEdit(row) {
      this.changeData(row);

      // 请求详情数据后赋值
      getDeviceTypesData(row.deviceTypeId).then(res => {
        if (200 === res.code) {
          this.formData = row;
          this.titleIndex = 1;
          this.dialogVisible = true;
        }
      })
    },
    // 删除数据
    handleDelete(row) {
      const ids = row.deviceTypeId ? [row.deviceTypeId] : this.ids;
      if (ids && ids.length) {
        this.$confirm('是否确认删除所选数据项？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          delDeviceTypesData(ids).then(res => {
            if (res.code == 200) {
              this.$message.success('删除成功');
              this.getList();
            }
          });
        });
      } else {
        this.$message.warning('请选择数据进行删除');
      }
    },
    // 查看详情
    handleDetail(row) {

      // 请求详情数据后赋值
      getDeviceTypesData(row.deviceTypeId).then(res => {
        if (200 === res.code) {
          this.descData = row;
          this.dialogVisibleDetail = true;
        }
      })
    },
    // 弹窗新增编辑-取消
    cancel() {
      this.dialogVisible = false;
    },
    // 弹窗详情-取消
    cancelDetail() {
      this.dialogVisibleDetail = false;
    },
    // 转值数据
    changeData(row) {
      row.num = +row.num;
      return row || null;
    },
    // 表单重置
    reset() {
      this.formData = {
        pid: 0,
        deviceTypeId: '',
        deviceTypeKey: '',
        deviceTypeName: '',
        sort: 0,
        num: '',
        remark: ''
      }
    },
    // 加载子节点
    loadChildren(row, treeNode, resolve) {
      this.searchMap.pid = row.deviceTypeId;
      listDeviceTypesData(this.searchMap).then(res => {
        if (200 === res.code) {
          this.searchMap.pid = 0
          resolve(
            res.data.map((item, index) => {
              if (item.leaf == '0') {
                item.hasChildren = true;
              }
              return item;
            })
          );
        } else {
        }
      });
    },
    // 打开查询子节点
    handleExpandChange(row, expanded) {
      if (!expanded) {
        let tableRef = this.$refs.table;
        tableRef.store.states.treeData[row.deviceTypeId].loaded = false;
        tableRef.store.states.treeData[row.deviceTypeId].expanded = false;
      }
    },
  },
};
</script>

<style lang="less" scoped></style>
